// 首页轮播图组件
<template>
  <swiper ref="swiper" v-if="banners.length">
    <swiper-item v-for="(item, index) in banners" :key="index">
      <a :href="item.link">
        <img :src="item.image" alt @load="loadImgCompleted" />
      </a>
    </swiper-item>
  </swiper>
</template>

<script>
import { Swiper, SwiperItem } from "components/common/swiper";

export default {
  name: "HomeSwiper",
  data() {
    return {
      //一张图片是否加载完成的标识
      isImgLoadFinish: true
    };
  },
  components: {
    Swiper,
    SwiperItem
  },
  props: {
    banners: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  methods: {
    //轮播图图片加载完成的事件
    loadImgCompleted() {
      if (this.isImgLoadFinish) {
        //当一张图片加载完成之后,触发父组件的图片加载完成方法
        this.$emit("imgLoadFinish");
        this.isImgLoadFinish = false;
      }
    },
    stopTimer() {
      this.$refs.swiper.stopTimer();
    },
    startTimer() {
      if (this.$refs.swiper) {
        this.$refs.swiper.startTimer();
      }
    }
  }
};
</script>

<style scoped>
</style>
